layui.define([], function (exports) {
  let $ = layui.$
    , wangEditor = {}


  wangEditor.render = function (options) {
    $.getScript(layui.cache.base + 'wangeditor@4.7.9.min.js', function () {
      let E = window.wangEditor
        , editor = new E(options.elem)

      // 设置编辑区域高度
      if (options.height) {
        editor.config.height = options.height
      }
      //自定义 placeholder 提示文字
      if (options.placeholder) {
        editor.config.placeholder = options.placeholder
      }
      // 取消自动 focus
      editor.config.focus = false
      // 配置 server 接口地址
      editor.config.uploadImgServer = options.url || 'admin/editor/image'
      // 一次最多上传5张图片
      editor.config.uploadImgMaxLength = 5
      // 自定义alert
      editor.config.customAlert = function (s, t) {
        layer.msg(s)
      }
      let inputElem = $('<textarea></textarea>');
      if (options.name) {
        // 自动创建textarea
        inputElem.attr('name', options.name)
        inputElem.hide()
        // 配置 onchange 回调函数
        $(options.elem).after(inputElem)
        editor.config.onchange = function (newHtml) {
          inputElem.html(newHtml)
        };
      }
      editor.create()
      inputElem.html(editor.txt.html())
    })

  }

  exports('wangEditor', wangEditor);
})
